package com.joinlabs.action;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLEncoder;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.joinlabs.entity.Address;
import com.joinlabs.entity.Order;
import com.joinlabs.entity.User;
import com.joinlabs.service.IOrderService;
import com.joinlabs.service.impl.OrderServiceImpl;
import com.joinlabs.service.impl.ShopcarServiceImpl;

@Controller
@RequestMapping("/order")
@SessionAttributes(names = {"user"})
public class OrderAction {

	@Resource(name = "orderServiceImpl")
	private IOrderService oService;

	/*
	 * 将选中的购物车项目生成订单并存入数据库 同时删除购物车选中的项目
	 * 
	 */
	@RequestMapping("/commit")
	public ModelAndView commitOrder(User user, BigDecimal[] checkname, ModelMap map) {
		// user.setUserId("123");
		// map.addAttribute("user",user);
		// int[] i ={1233};
		Order orders = oService.commitOrder(checkname, user);
		// System.out.println(orders);
		List<Address> addrs = oService.selectAllAddress(user);
		//System.out.println("=======" + Arrays.toString(checkname));
		// addrs.add(new Address());
		// addrs.add(new Address());
		// addrs.remove(0);
		// addrs.remove(1);
		// System.out.println(addrs.size());

		map.addAttribute("orders", orders);
		map.addAttribute("addrs", addrs);
		
		return new ModelAndView("/order/commitOrder");
	}
	
	/*
	 * 付款成功后跳转到提示页面
	 */
	/*
	 * @RequestMapping("/paySec") public ModelAndView paySuccess(String[] oid) {
	 * oService.payOver(oid);
	 * 
	 * return new ModelAndView("/order/paySec"); }
	 */
	/*
	 * 向订单记录添加地址 并跳转到支付界面
	 * 
	 */
	/*
	 * @RequestMapping("/pay") public ModelAndView pay(String oid,long address) {
	 * 
	 * oService.pay(oid, address);
	 * 
	 * System.out.println("====================="); System.out.println(oid);
	 * System.out.println(address); System.out.println("=====================");
	 * 
	 * return null; }
	 */
	
	
	//取出该用户所有的order
	@RequestMapping("/show")
	public ModelAndView showOrder(User user, ModelMap map) {
		// user.setUserId("123");
		List<Order> orderList = oService.selcetByUser(user.getUserId());
		System.out.println(orderList);
		map.addAttribute("orderList", orderList);

		return new ModelAndView("/user/order");

	}
	
	
	//确认收货
	@RequestMapping("/takeover")
	public ModelAndView takeOver(String orderId, User user, ModelMap map) {
		oService.takeOver(orderId);
		return new ModelAndView("redirect:/order/show");
	}
	
	//对订单界面未支付的订单进行支付
	@RequestMapping("/pay")
	public ModelAndView pay(String orderId, User user, ModelMap map) {
		List<Address> addrs = oService.selectAllAddress(user);
		Order order = oService.selectOrderByPK(orderId);
		map.addAttribute("orders", order);
		map.addAttribute("addrs", addrs);
		return new ModelAndView("/order/commitOrder");
	}
	

	
	@RequestMapping("/delete")
	public ModelAndView deleteOrder(String orderId, User user, ModelMap map) {
		oService.delete(orderId);
		return new ModelAndView("redirect:/order/show");
	}
}
